from google.appengine.ext import webapp
from DataClass.WebsiteRec import WebsiteRec
from google.appengine.api import users
from google.appengine.ext.webapp import template
from AdminBase import AdminBase
from DataClass.FullPage import FullPage

import os
import cgi 

class AdminListBase(AdminBase):
    def GetBreadcrumbLink(self):
        raise Exception("GetBreadcrumbLink not implemented")

    def GetBreadcrumbDisplayName(self):
        raise Exception("GetBreadcrumbDisplayName not implemented")

    def GetListHeading(self):
        raise Exception("GetListHeading not implemented")

    def GetSingleItemDisplayName(self):
        raise Exception("GetSingleItemDisplayName not implemented")

    def GetSingleItemLink(self):
        raise Exception("GetSingleItemLink not implemented")
    
    # return the template_values. Optional to override this.
    def GetExtraTemplateValues(self, template_values, user):
        return template_values

    # return a query on the items that we want
    def GetListQuery(self, template_values, user):
        raise Exception("GetListQuery not implemented")

    def GetItemTemplateFileName(self):
        raise Exception("GetItemTemplateFileName not implemented")


    def GetTemplateFileName(self):
        return 'PSPages/adminlist.html'
    
    def RenderItem(self, aItem, template_values):
        path = os.path.join(os.path.dirname(__file__), self.GetItemTemplateFileName())
        item_template_values = {}
        item_template_values['item'] = aItem
        item_template_values.update(template_values)
        renderedhtml = template.render(path, item_template_values)
        return renderedhtml
    
    def GetBreadcrumbList(self):
        return [['main', 'admin'],[self.GetBreadcrumbDisplayName(), self.GetBreadcrumbLink()]]
    
    def GetTemplateValuesDict(self, template_values, user):
        lentv = len(template_values)
        
        querystring=cgi.FieldStorage()
        try:
            offset=int(querystring.getvalue('offset'))
        except:
            offset = 0
        try:
            amount=int(querystring.getvalue('amount'))
        except:
            amount = 20
        
        template_values = self.GetExtraTemplateValues(template_values, user)

        TheListQuery = self.GetListQuery(template_values, user) 
        
        if TheListQuery:
            TheList = TheListQuery.fetch(amount, offset)
        else: 
            TheList = []
            
        # here we have the list. Now, turn this into a list of item templates
        TheHtmlList = []
        for item in TheList:
            itemHtml = self.RenderItem(item, template_values)        
            TheHtmlList.append(itemHtml)

        template_values['thehtmllist'] = TheHtmlList
        template_values['singleitemdisplayname'] = self.GetSingleItemDisplayName()
        template_values['listheading'] = self.GetListHeading()
        template_values['singleitemlink'] = self.GetSingleItemLink()
        template_values['selflink'] = self.GetBreadcrumbLink()
        template_values['amount'] = amount
        template_values['neednext'] = len(TheList) == amount
        template_values['needprev'] = offset != 0
        template_values['nextoffset'] = offset + amount
        template_values['prevoffset'] = offset - amount
        
        retval = template_values
                    
        return retval
